﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emDocFile : emMasterDetailEdit
    {
        private data.baseDS.emFileDataTable myDataTbl = new data.baseDS.emFileDataTable();
        public emDocFile()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);;
            }            
        }

        public override event EventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                emDocFileDetail.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
                emDocFileDetail.myOnError -= value;
            }
        }


        public override void Init()
        {
            base.Init();
            emDocFileDetail.Init();
            SetData(myDataTbl);
            myDataTbl.Clear();
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                application.dataLibs.LoadData(myDataTbl, emCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            application.dataLibs.UpdateData(myDataTbl,emCode);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state);
            emDocFileDetail.LockEdit(state);
        }
        public override bool CheckData()
        {
            return emDocFileDetail.CheckData();
        }
        public override void SetFocus()
        {
            emDocFileDetail.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emFileRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emFileRow)((DataRowView)this.gridDataSource.Current).Row;
                application.dataLibs.InitData(row);
            }
            emDocFileDetail.SetDataSource(gridDataSource);
        }

        private void SetData(data.baseDS.emFileDataTable tbl)
        {
            gridDataSource.DataSource = tbl;
            emDocFileDetail.SetDataSource(gridDataSource);
            DataGridViewCellStyle numberCellType = new DataGridViewCellStyle();
            numberCellType.Format = "N0";
            numberCellType.Alignment = DataGridViewContentAlignment.MiddleRight; 

            DataGridViewTextBoxColumn fileDescColumn = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn fileSizeColumn = new DataGridViewTextBoxColumn();
            this.detailGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { fileDescColumn, fileSizeColumn }
                                         );
            // 
            // fileDescColumn
            // 
            fileDescColumn.DataPropertyName = tbl.descriptionColumn.ColumnName;
            fileDescColumn.HeaderText = "Tệp";
            fileDescColumn.Name = tbl.sizeKBColumn.ColumnName;
            fileDescColumn.ReadOnly = true;
            // 
            // fileSizeColumn
            // 
            fileSizeColumn.DataPropertyName = tbl.sizeKBColumn.ColumnName;
            fileSizeColumn.HeaderText = "KB";
            fileSizeColumn.Width = 80;
            fileSizeColumn.DefaultCellStyle = numberCellType;
            fileSizeColumn.Name = tbl.sizeKBColumn.ColumnName;
            fileSizeColumn.ReadOnly = true;

            common.system.AutoFitGridColumn(detailGrid, fileDescColumn.Name);
        }

        private void emDocFileDetail_myOnError(object sender, Exception er)
        {
            //this.myOnError(sender, er);
        }
    }
}
